package com.je.core.base;

import com.google.common.base.Joiner;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.util.HashMap;
import java.util.Map;

/**
 * 描述: Request包装类
 *
 * @auther: wangmm@ketr.com.cn
 * @date: 2019/3/5 20:19
 */
public class JERequestWrapper extends HttpServletRequestWrapper {

    private Map<String, String[]> parameterMap;

    private HttpServletRequest originalRequest;

    public JERequestWrapper(HttpServletRequest request) {
        super(request);
        setOriginalRequest(request);
        parameterMap = new HashMap<>(request.getParameterMap());
    }

    public JERequestWrapper(HttpServletRequest request, HttpServletRequest originalRequest) {
        super(request);
        setOriginalRequest(originalRequest);
        parameterMap = new HashMap<>(request.getParameterMap());
    }

    public HttpServletRequest getOriginalRequest() {
        return originalRequest;
    }

    public void setOriginalRequest(HttpServletRequest originalRequest) {
        this.originalRequest = originalRequest;
    }

    @Override
    public String[] getParameterValues(String name) {
        return parameterMap.get(name);
    }

    @Override
    public String getHeader(String name) {
        return super.getHeader(name);
    }

    /**
     * 描述: 重写获取参数方法
     * @auther: wangmm@ketr.com.cn
     * @date: 2019/3/5 20:36
     */
    @Override
    public String getParameter(String name) {
        String[] values = parameterMap.get(name);
        if(values == null || values.length<=0){
            return null;
        }
        if (values.length == 1) {
            return values[0];
        }
        return Joiner.on(",").join(values);
    }

    /**
     * 描述: 添加设置参数方法
     *
     * @auther: wangmm@ketr.com.cn
     * @date: 2019/3/5 20:36
     */
    public void setParameter(String key, String value) {
        String[] values = new String[]{value};
        parameterMap.put(key, values);
    }

    public void setParameter(String key, String[] values) {
        parameterMap.put(key, values);
    }

    @Override
    public Map<String, String[]> getParameterMap() {
        return this.parameterMap;
    }

}
